package com.prezs.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.zs.mp.enums.Gender;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class cusEnumTypeHandler extends BaseTypeHandler<Gender>{

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Gender parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setString(i, parameter.getGenderCode());
		log.info("{}",parameter);
	}

	@Override
	public Gender getNullableResult(ResultSet rs, String columnName) throws SQLException {
		log.info("{}", rs,columnName);
		Object object = rs.getObject(columnName);
		String genderCode =  object != null && object instanceof String?(String)object:null;
		return Gender.getGenderValue(genderCode);
	}

	@Override
	public Gender getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		
		log.info("{}", columnIndex);
		Object object = rs.getObject(columnIndex);
		
		String genderCode = object != null && object instanceof String ? (String)object :null;
		
		return Gender.getGenderValue(genderCode);
	}

	@Override
	public Gender getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		log.info("{}", columnIndex);
		Object object = cs.getObject(columnIndex);
		
		String genderCode = object != null && object instanceof String ? (String)object :null;
		
		return Gender.getGenderValue(genderCode);
	}

}
